"""
### 4.合并数组
* 给你两个按`非递减顺序`排列的整数数组nums1和nums2，另有两个整数m和n，
  分别表示nums1和nums2中的元素数目。
* 请你合并nums2到nums1中，使合并后的数组同样按`非递减顺序`排列。
* 注意：最终，合并后数组不应由函数返回，而是存储在数组 nums1 中。
  为了应对这种情况，nums1 的初始长度为 m + n，其中前 m 个元素表示应合并的元素，后 n 个元素为 0 ，应忽略。nums2 的长度为 n 。
"""


def join1(nums1: list, m: int, nums2: list, n: int) -> list:
    # 用nums2的数据将nums1后面的0填充
    for i in range(m, m + n):
        nums1[i] = nums2[i - m]
    # 内建排序函数
    # nums1.sort()
    # return nums1
    return sorted(nums1)


# num1 = [1, 2, 3, 0, 0, 0]
# m1 = 3
# num2 = [2, 5, 6]
# n1 = 3

while True:
    button = input("是否继续运行程序，按q退出:")
    if button == 'q':
        break
    try:
        # 用map将每个输入都转换成int，将map的类型转换成list
        num1 = list(map(int, input("请输入nums1的值(数字列表):").split(" ")))
        m1 = int(input("请输入nums1中不为0的个数:"))
        num2 = list(map(int, input("请输入nums2的值(数字列表):").split(" ")))
        n1 = int(input("请输入nums2中不为0的个数:"))
        num11 = join1(num1, m1, num2, n1)
        print(f"合并的数组:{num11}")
    except Exception as ex:
        print("输入错误，请重新输入", ex)


# 重新创建了一个数组不符合要求，要nums1为最后的输出结果
# def join2(nums1: list, m: int, nums2: list, n: int) -> list:
#     left = 0
#     right = 0
#     nums = []
#     while left < m and right < n:
#         if nums1[left] > nums2[right]:
#             nums.append(nums2[right])
#             right = right + 1
#         else:
#             nums.append(nums1[left])
#             left = left + 1
#     while left < m:
#         nums.append(nums1[left])
#         left += 1
#     while right < n:
#         nums.append(nums2[right])
#         right += 1
#     return nums
#
#
# # num1 = [1]
# # m1 = 1
# # num2 = [0]
# # n1 = 0
# num1 = [-1, 0, 0, 3, 3, 3, 0, 0, 0]
# m1 = 6
# num2 = [1, 2, 2]
# n1 = 3
# for i in range(m1, len(num1)):
#     num1[i] = float('inf')
# print(num1)
# num22 = join2(num1, m1, num2, n1)
# print(f"合并的数组:{num22}")

# def join2(nums1: list, m: int, nums2: list, n: int) -> list:
#     left = 0
#     right = 0
#     i = 0
#     nums = nums1[::]
#     while left < m and right < n:
#         if nums[left] > nums2[right]:
#             nums1[i] = nums2[right]
#             right = right + 1
#         else:
#             nums1[i] = nums[left]
#             left = left + 1
#         i = i + 1
#     while left < m:
#         nums1[i] = nums[left]
#         left += 1
#         i = i + 1
#     while right < n:
#         nums1[i] = nums2[right]
#         right += 1
#         i = i + 1
#     return nums1
#
#
# num1 = [-1, 0, 0, 3, 3, 3, 0, 0, 0]
# m1 = 6
# num2 = [1, 2, 2]
# n1 = 3
# for i in range(m1, len(num1)):
#     num1[i] = float('inf')
# print(num1)
# num22 = join2(num1, m1, num2, n1)
# print(f"合并的数组:{num22}")
